<template>
	<view class="evaluate-container page-container">

		<!-- 订单信息 -->
		<view class="order-section">
			<view class="order-header">
				<text class="order-title">订单信息</text>
			</view>
			<view class="order-info">
				<text class="order-no">订单号：{{ orderInfo.order_no }}</text>
				<text class="order-time">下单时间：{{ orderInfo.create_time }}</text>
			</view>
		</view>

		<!-- 商品评价 -->
		<view class="product-section" v-for="(product, index) in orderInfo.products" :key="product.id">
			<view class="product-header">
				<text class="product-title">评价商品：{{ product.spu_name }}</text>
			</view>
			<view class="product-item">
				<image :src="$tools.buildUrl(product.main_image)" mode="aspectFill" class="product-image"></image>
				<view class="product-details">
					<text class="product-name">{{ product.spu_name }}</text>
					<text class="product-spec">{{ product.sku_name }}</text>
				</view>
			</view>

			<!-- 商品评分 -->
			<view class="rating-section">
				<text class="rating-label">商品评分：</text>
				<view class="stars">
					<u-icon v-for="n in 5" :key="n" :name="n < product.star_product ? 'star-fill' : 'star'" size="24"
						:color="n < product.star_product ? '#ffa500' : '#e5e5e5'"
						@click.stop="setProductRating(product, n+1)"></u-icon>
				</view>
				<text class="rating-text">{{ getRatingText(product.star_product) }}</text>
			</view>

			<!-- 评价内容 -->
			<view class="comment-section">
				<textarea class="comment-input" placeholder="分享您的使用感受" v-model="product.comment_text"
					maxlength="500"></textarea>
				<text class="comment-count">{{ product.comment_text.length }}/500</text>
			</view>

			<!-- 上传图片 -->
			<view class="image-section">
				<text class="image-label">上传图片（最多9张）</text>
				<view class="image-list">
					<view class="image-item" v-for="(image, imgIndex) in product.comment_images" :key="imgIndex">
						<image :src="$tools.buildUrl(image)" mode="aspectFill" class="upload-image"></image>
						<view class="image-delete" @click="deleteImage(index, imgIndex)">
							<u-icon name="close" size="16" color="#fff"></u-icon>
						</view>
					</view>
					<view class="image-add" @click="chooseImage(product)" v-if="product.comment_images.length < 9">
						<u-icon name="plus" size="32" color="#999"></u-icon>
					</view>
				</view>
			</view>
		</view>

		<!-- 整体评价 -->
		<view class="overall-section">
			<view class="section-header">
				<text class="section-title">整体评价</text>
			</view>

			<view class="overall-rating">
				<view class="rating-item">
					<text class="rating-label">快递包装：</text>
					<view class="stars">
						<u-icon v-for="n in 5" :key="n" :name="n < overallRating.star_package ? 'star-fill' : 'star'"
							size="24" :color="n < overallRating.star_package ? '#ffa500' : '#e5e5e5'"
							@click="setOverallRating('star_package', n+1)"></u-icon>
					</view>
					<text class="rating-text">{{ getRatingText(overallRating.star_package) }}</text>
				</view>

				<view class="rating-item">
					<text class="rating-label">物流速度：</text>
					<view class="stars">
						<u-icon v-for="n in 5" :key="n" :name="n < overallRating.star_logistics ? 'star-fill' : 'star'"
							size="24" :color="n < overallRating.star_logistics ? '#ffa500' : '#e5e5e5'"
							@click="setOverallRating('star_logistics', n+1)"></u-icon>
					</view>
					<text class="rating-text">{{ getRatingText(overallRating.star_logistics) }}</text>
				</view>

				<view class="rating-item">
					<text class="rating-label">配送服务：</text>
					<view class="stars">
						<u-icon v-for="n in 5" :key="n" :name="n < overallRating.star_delivery ? 'star-fill' : 'star'"
							size="24" :color="n < overallRating.star_delivery ? '#ffa500' : '#e5e5e5'"
							@click="setOverallRating('star_delivery', n+1)"></u-icon>
					</view>
					<text class="rating-text">{{ getRatingText(overallRating.star_delivery) }}</text>
				</view>
			</view>
		</view>

		<!-- 底部按钮 -->
		<view class="bottom-actions">
			<button class="action-btn secondary" @click="goBack">返回</button>
			<button class="action-btn primary" @click="submitEvaluate">提交评价</button>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex'
	import projectConfig from '@/common/config.js';

	export default {
		data() {
			return {
				orderId: '',
				orderInfo: {},
				overallRating: {
					star_package: 0,
					star_package_desc: '',
					star_logistics: 0,
					star_logistics_desc: '',
					star_delivery: 0,
					star_delivery_desc: ''
				}
			}
		},
		computed: {
			...mapGetters(['isLogin', 'userInfo']),
		},
		onLoad(options) {
			if (options.orderId) {
				this.orderId = options.orderId
				this.loadOrderInfo()
			}
		},
		methods: {
			// 加载订单信息
			loadOrderInfo() {
				this.$api.order.GetOrdersInfo({
					id: this.orderId
				}).then(res => {
					res.data.products.forEach(item => {
						item.comment_text = ''
						item.comment_images = []
						item.star_product = 0
						item.star_product_desc = ''
					})
					this.orderInfo = res.data
				})
			},

			// 设置商品评分
			setProductRating(product, rating) {
				product.star_product = rating
				product.star_product_desc = this.getRatingText(rating)
			},

			// 设置整体评分
			setOverallRating(type, rating) {
				this.overallRating[type] = rating
				this.overallRating[type + '_desc'] = this.getRatingText(rating)
			},

			// 获取评分文本
			getRatingText(rating) {
				const texts = ['', '很差', '一般', '满意', '很好', '非常好']
				return texts[rating] || ''
			},

			// 选择图片
			chooseImage(product) {
				const maxCount = 9 - product.comment_images.length
				uni.chooseImage({
					count: maxCount,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						res.tempFilePaths.forEach(tempFilePath => {
							this.uploadPhoto(product, tempFilePath);
						})
					}
				})
			},

			// 上传图片到服务器
			uploadPhoto(product, imageUrl) {
				uni.showLoading({
					title: '正在上传'
				})
				const uploadUrl = '/api/App_Upload/UploadFile';
				const headerConfig = this.$sign.buildHeaderSign(uploadUrl, this.userInfo, {}, 'POST'); // 生成签名
				//上传到服务器
				uni.uploadFile({
					url: projectConfig.baseUrl + uploadUrl,
					filePath: imageUrl, //要上传文件资源的路径 (本地路径)
					name: 'photo', //文件对应的 key，开发者在服务端可以通过这个 key 获取文件的二进制内容
					header: headerConfig,
					formData: {}, //HTTP 请求中其他额外的 form data       
					success: res => {
						uni.hideLoading()
						var jsonObj = this.$tools.parseJsonObject(res.data)
						if (jsonObj.code == 100) {
							//保存成功 
							product.comment_images.push(jsonObj.data.relativeUrl)
						} else {
							//保存失败							
							uni.showToast({
								title: jsonObj.msg,
								icon: 'error'
							})
						}
					},
					fail: err => {
						uni.hideLoading()
						uni.showToast({
							title: '上传图片错误：' + JSON.stringify(err),
							icon: 'error'
						})
					}
				})
			},

			// 删除图片
			deleteImage(productIndex, imageIndex) {
				this.orderInfo.products[productIndex].comment_images.splice(imageIndex, 1)
			},

			// 提交评价
			submitEvaluate() {
				// 验证评价内容
				for (let i = 0; i < this.orderInfo.products.length; i++) {
					const product = this.orderInfo.products[i]
					if (product.star_product === 0) {
						uni.showToast({
							title: `请为${product.spu_name}/${product.sku_name}评分`,
							icon: 'none'
						})
						return
					}
				}

				if (this.overallRating.star_package === 0) {
					uni.showToast({
						title: '请为快递包装评分',
						icon: 'none'
					})
					return
				}

				if (this.overallRating.star_logistics === 0) {
					uni.showToast({
						title: '请为物流速度评分',
						icon: 'none'
					})
					return
				}

				if (this.overallRating.star_delivery === 0) {
					uni.showToast({
						title: '请为配送服务评分',
						icon: 'none'
					})
					return
				}

				// 提交评价
				uni.showLoading({
					title: '提交中...'
				})

				this.$api.order.CommentOrders({
					order_id: this.orderId,
					commentArray: this.orderInfo.products,
					overallRating: this.overallRating,
					comment_nickname: this.userInfo.nickname,
					comment_avatar: this.userInfo.avatar_url
				}).then(res => {
					uni.hideLoading()
					uni.showToast({
						title: res.msg,
						icon: 'success'
					})
					const pages = getCurrentPages()
					const prevPage = pages[pages.length - 2]
					if (prevPage) {
						prevPage.$vm.setComment(this.orderId)
					}
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)
				})
			},

			// 返回
			goBack() {
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.evaluate-container {
		background-color: #f8f8f8;
		min-height: 100vh;
		padding-bottom: 120rpx;
	}

	.order-section,
	.product-section,
	.overall-section {
		background-color: #ffffff;
		margin: 20rpx;
		border-radius: 16rpx;
		padding: 40rpx;
	}

	.order-header,
	.product-header,
	.section-header {
		margin-bottom: 30rpx;
	}

	.order-title,
	.product-title,
	.section-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
	}

	.order-info {
		padding-left: 20rpx;
	}

	.order-no,
	.order-time {
		font-size: 26rpx;
		color: #666;
		margin-bottom: 12rpx;
		display: block;
	}

	.product-item {
		display: flex;
		align-items: center;
		margin-bottom: 30rpx;
	}

	.product-image {
		width: 120rpx;
		height: 120rpx;
		border-radius: 12rpx;
		margin-right: 20rpx;
	}

	.product-details {
		flex: 1;
	}

	.product-name {
		font-size: 28rpx;
		color: #333;
		margin-bottom: 12rpx;
		display: block;
	}

	.product-spec {
		font-size: 24rpx;
		color: #999;
		display: block;
	}

	.rating-section {
		display: flex;
		align-items: center;
		margin-bottom: 30rpx;
	}

	.rating-label {
		font-size: 28rpx;
		color: #333;
		margin-right: 20rpx;
	}

	.stars {
		display: flex;
		margin-right: 20rpx;
	}

	.rating-text {
		font-size: 26rpx;
		color: #ffa500;
	}

	.comment-section {
		margin-bottom: 30rpx;
		position: relative;
	}

	.comment-input {
		// width: 100%;
		min-height: 200rpx;
		padding: 24rpx;
		border: 2rpx solid #e5e5e5;
		border-radius: 12rpx;
		font-size: 28rpx;
		line-height: 1.5;
	}

	.comment-count {
		position: absolute;
		bottom: 20rpx;
		right: 20rpx;
		font-size: 24rpx;
		color: #999;
	}

	.image-section {
		margin-bottom: 30rpx;
	}

	.image-label {
		font-size: 28rpx;
		color: #333;
		margin-bottom: 20rpx;
		display: block;
	}

	.image-list {
		display: flex;
		flex-wrap: wrap;
	}

	.image-item {
		width: 160rpx;
		height: 160rpx;
		position: relative;
		margin-right: 20rpx;
		margin-bottom: 20rpx;
	}

	.upload-image {
		width: 100%;
		height: 100%;
		border-radius: 12rpx;
	}

	.image-delete {
		position: absolute;
		top: -10rpx;
		right: -10rpx;
		width: 40rpx;
		height: 40rpx;
		background-color: #ff4757;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.image-add {
		width: 160rpx;
		height: 160rpx;
		border: 2rpx dashed #e5e5e5;
		border-radius: 12rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.overall-rating {
		margin-top: 20rpx;
	}

	.rating-item {
		display: flex;
		align-items: center;
		margin-bottom: 30rpx;
	}

	.rating-item:last-child {
		margin-bottom: 0;
	}

	.bottom-actions {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background-color: #ffffff;
		border-top: 2rpx solid #e5e5e5;
		padding: 30rpx;
		display: flex;
	}

	.action-btn {
		flex: 1;
		border: none;
		border-radius: 40rpx;
		padding: 0 20rpx;
		font-size: 28rpx;
		margin: 0 10rpx;
	}

	.action-btn:first-child {
		margin-left: 0;
	}

	.action-btn:last-child {
		margin-right: 0;
	}

	.action-btn.primary {
		background-color: #1c705e;
		color: #ffffff;
	}

	.action-btn.secondary {
		background-color: transparent;
		color: #666;
		border: 2rpx solid #e5e5e5;
	}
</style>